package org.mspring.platform.dao.query.convert.impl;

import org.mspring.platform.dao.query.convert.ConversionException;
import org.mspring.platform.dao.query.convert.Converter;
import org.mspring.platform.utils.DateUtils;

/**
 * @author Gao Youbo
 * @since Jan 31, 2012
 */
public class DateConverter implements Converter {

    @Override
    public Object convert(Class<?> type, String value) throws ConversionException {
        if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
            return null;
        }
        int datelength = value.length();
        String pattern = "";
        if (datelength > 10) {
            pattern = DateUtils.YYYY_MM_DD_HH_MM_SS;
        } else if (datelength == 10) {
            pattern = "yyyy-MM-dd";
        } else {
            pattern = "yyyyMMdd";
        }
        return DateUtils.parse(value, pattern);
    }

}
